package net.sharpf2.api.interfaces;

import net.sharpf2.sfcore.client.Interface;
import net.sharpf2.api.Globals;
import net.sharpf2.api.game.Interfaces;
import net.sharpf2.api.types.Content;

/**
 * Class to handle the combat tab.
 * @author Piotr Gajdowski
 */
public class Combat {
    
    /**
     * Return the actual interface object for the combat tab.
     * @return Content<Interface>
     */
    public static Content<Interface> getInterface() {
        return Interfaces.get(Constants.COMBAT_TAB, 0);
    }
    
    /**
     * Returns the children of the combat tab.
     * @param   child Child ID.
     * @return  Content<Interface>
     */
    public static Content<Interface> getChild(int child) {
        return Interfaces.get(Constants.COMBAT_TAB, child);
    }
    
    /**
     * Method to click a button in the Combat tab.
     * @param   button  ID of the button, use the Constants class to indicate the ID or you can use the actionSearcher(String s) method.
     * @return  Interface
     */
    public static Interface clickButton(int button) {
        Tabs.checkAndOpen(Tabs.Constants.TAB_ATTACK);
        Content<Interface> btn = getChild(button);
        if (btn != null) {
            btn.getActions().click();
        }
        return null;
    }
    
    /**
     * Retrieves the weapon name
     * @return  the name of the weapon
     */
    public static String getWeaponName() {
        Tabs.checkAndOpen(Tabs.Constants.TAB_ATTACK);
        Content<Interface> weaponName = getChild(Constants.WEAPON_NAME);
        if (weaponName != null) {
            return weaponName.getContent().getText();
        }
        return null;
    }
    
    /**
     * Method to click the special attack bar
     */
    public static void doSpecial() {
        Tabs.checkAndOpen(Tabs.Constants.TAB_ATTACK);
        Content<Interface> specialBar = getChild(Constants.SPECIAL_ATTACK);
        if (specialBar != null) {
            specialBar.getActions().click();
        }
    }
    
    /**
     * Retrieves the special power left
     * @return  special power
     */
    public static int getSpecialValue() {
        return Globals.CLIENT.getSettings().getData()[300];
    }
    
    /**
     * Searches if any of the interfaces child contains the given action.
     * @param s Action
     * @return The child id with the containing action string.
     */
    public static int actionSearcher(String s) {
        Tabs.checkAndOpen(Tabs.Constants.TAB_ATTACK);
        for (int i=0; i <= Constants.BOX.length; i++) {
            Content<Interface> box = getChild(Constants.BOX[i]);
            for (int j=0; j<=box.getContent().getActions().length; j++) {
                if (box.getContent().getActions()[j].contains(s)) {
                    return Constants.BOX[i];
                }
            }
        }
        return -1;
    }
    
    /**
     * Toggles Auto Retaliate on/off.
     * @param onoff true to set auto retaliate on, otherwise false.
     */
    public static void toggleAutoRetaliate(boolean onoff) {
        int oo = onoff ? 0 : 1;
        if (Globals.CLIENT.getSettings().getData()[Constants.SETTING_AUTO_RETALIATE] != oo) {
            Tabs.checkAndOpen(Tabs.Constants.TAB_ATTACK);
            Interfaces.get(Constants.COMBAT_TAB, Constants.BOX_AUTO_RETALIATE).getActions().click(Constants.BOX_AUTO_RETALIATE_ACTION);
        }
    }
    
    /**
     * Gets the current combat style (0-3), style 4 is magic.
     * @return The current combat style.
     */
    public static int getCurrentCombatStyle() {
        return Globals.CLIENT.getSettings().getData()[43];
    }
    
    public static class Constants {
        public final static int COMBAT_TAB = /*387*/884;
        public final static int WEAPON_NAME = 0;
        public final static int SPECIAL_ATTACK = 4;
        public final static int BOX_LEFT_TOP = 11;
        public final static int BOX_LEFT_BOTTOM = 13;
        public final static int BOX_RIGHT_TOP = 12;
        public final static int BOX_RIGHT_BOTTOM = 14;
        public final static int BOX_AUTO_RETALIATE = 15;
        public final static int SETTING_AUTO_RETALIATE = 172;
        public final static int[] BOX = { 11, 12, 13, 14 };
        public final static String BOX_AUTO_RETALIATE_ACTION = "(Auto Retaliate|Auto-Kampf|Combat automatique)";
    }
    
}
